package jdk8.lambda;

import org.junit.Test;

interface MyMath{
	public abstract int add(int a,int b);
}

interface MyString{
	// 把s1和s2的长度和返回
	int getAllLength(String s1,String s2);
}

public class LambdaDemo {
	
	@Test
	public void t5() {
		MyString ms = (s1,s2) -> s1.length() + s2.length();
		int allLength = ms.getAllLength("abc", "defg");
		System.out.println(allLength);
		
		MyString ms2 = (a,b) -> a.length()*2 + b.length()*3;
		int allLength2 = ms2.getAllLength("abc", "defg");
		System.out.println(allLength2);
	}

	@Test
	public void t1() {
		// 匿名内部类
		MyMath m = new MyMath() {
			@Override
			public int add(int a, int b) {
				return a + b;
			}
		};
		System.out.println(m.add(1, 2));
	}
	
	@Test
	public void t2() {
		// 一个匿名函数去给MyMath里的唯一的抽象方法赋值
		MyMath m = (int a, int b)->{
				return a + b;
		};
		System.out.println(m.add(3, 4));
	}
	
	@Test
	public void t3() {
		// 参数类型可省
		MyMath m = (a,b)->{
				return a + b;
		};
		System.out.println(m.add(3, 4));
	}
	
	@Test
	public void t4() {
		// 如果函数体里面就一句话，{}和return可省
		MyMath m = (a,b) -> a + b;
		System.out.println(m.add(13, 14));
	}
}
